package com.csx.governance.traffic.condition.support;

import cn.hutool.core.util.ObjectUtil;
import com.csx.governance.traffic.condition.ParsedExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @author cuisongxu
 * @date 2023/2/25 13:50
 */
public class ConditionMatcher {

    private static final Logger logger = LoggerFactory.getLogger(ConditionMatcher.class);

    public static boolean match(ParsedExpression parsedExpression, Map<String, Object> params) {

        // 无条件直接返回true
        if(ObjectUtil.isEmpty(parsedExpression)) {
            return true;
        }
        // 有条件则尝试进行匹配
        try {
            return ExpressionExecutors.FIRST_EXPRESSION_EXECUTOR.execute(parsedExpression, params, Boolean.class);
        } catch (Throwable e) {
            logger.debug("Expression {} match error because: ", parsedExpression, e);
            return false;
        }
    }
}
